
const express = require('express');
// const session = require('express-session');
const app = express();
const http = require('http');
const path = require('path');
const socketIo = require('socket.io')
const server = http.createServer(app);
// const sessionMiddleware = session({ secret: 'keyboard cat', cookie: { maxAge: 60000 } });
// app.use(sessionMiddleware);
app.use(express.static(path.resolve(__dirname, 'public')))
server.listen(5008, () => console.log('server 5008'));

// websocket
const io = socketIo(server);
io.on('connection', (socket) => {
    // 当有一个新的客户端连接到服务器触发
    // const session = socket.request.session;
    // session.connections++;
    // session.save();

    const timer = setInterval(() => {
        socket.emit("test", "test msg")
    }, 2000);

    socket.on("test", data => { //监听客户端的消息
        console.log('test', data);
    })

    socket.on("disconnect", data => { //监听客户端断开
        console.log('客户端断开');
        clearInterval(timer)
    })
});

// // register middleware in Socket.IO
// io.use((socket, next) => {
//     sessionMiddleware(socket.request, {}, next);
//     // sessionMiddleware(socket.request, socket.request.res, next); will not work with websocket-only
//     // connections, as 'socket.request.res' will be undefined in that case
// });
